﻿# -*- coding: utf-8 -*-
"""
下载群文件中的 access_2021_08_30.log 文件，通过正则表达式统计访问日志中 http 返回码及对应的个数，访问的 URL 以及个数
改用生产者消费者模式
"""
import queue
import re
from pathlib import Path
from collections import Counter


def get(p, reverse=False):
    Code = []
    path = []
    path_len = 0
    with open(p, 'r') as file:
        a = file.readline()
        while a:
            r = re.search('\s(\d+)\s[^\"]+\"([^\"]+)', a)
            Code.append(r.group(1))
            P = r.group(2)
            if P != '-':
                path.append(P)
            if len(P) > path_len:
                path_len = len(P)
            a = file.readline()
    Code = Counter(Code)
    Code = sorted(Code.items(), key=lambda i: i[1], reverse=reverse)
    # print(Code,type(Code))
    q_code = queue.Queue()
    for n in Code:
        q_code.put(n)
    path = Counter(path)
    path = sorted(path.items(), key=lambda i: i[1], reverse=reverse)
    q_path = queue.Queue()
    for m in path:
        q_path.put(m)
    return q_code, q_path, path_len + 2


if __name__ == '__main__':
    p = Path('./access_2021_08_30.log')
    q_code, q_path, Len = get(p)
    print('返回码及对应的个数:')
    while not q_code.empty():
        Code = q_code.get()
        print(Code[0], '---->', Code[1])
    print('—' * 20, '\nURL 以及个数:')
    while not q_path.empty():
        path = q_path.get()
        print('{:<{}}---->{}'.format(path[0], Len, path[1]))
# 可以优化下输出格式，比如使用字典